package com.kefu.call.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.call.mapper.CallMakeCallMapper;
import com.kefu.call.mapper.CallVoiceMailMapper;
import com.kefu.call.mapper.ContactPlanRelMapper;
import com.kefu.call.model.CallMakeCall;
import com.kefu.call.model.CallVoiceMail;
import com.kefu.call.model.ContactPlanRel;
import com.kefu.call.vo.ContactPlanSearchVo;
import com.kefu.call.vo.ContactPlanUpdateVo;
import com.kefu.common.util.JacksonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kefu.basic.mapper.BasicMsgMapper;
import com.kefu.basic.model.BasicMsg;
import com.kefu.call.mapper.ContactPlanMapper;
import com.kefu.call.model.ContactPlan;
import com.kefu.call.vo.ContactPlanInsertVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author francis
 * @date 2021/05/22
 */
@Service
@Transactional
public class ContactPlanService  extends AbstractKefuService<ContactPlan> {
	@Resource
	private CallMakeCallMapper callMakeCallMapper;
	@Resource
	protected BasicMsgMapper basicMsgMapper;
	@Resource
    protected ContactPlanMapper contactPlanMapper;
	@Resource
	protected CallVoiceMailMapper callVoiceMailMapper;
	@Resource
	protected ContactPlanRelMapper contactPlanRelMapper;


	
	public String addContactPlan(ContactPlanInsertVo contactPlanVo) {
		ContactPlan info = contactPlanVo.convert();
		save(info);
		return info.getPlanId();
	}

	public String updateContactPlan(ContactPlanUpdateVo contactPlanVo) {
		ContactPlan info = contactPlanVo.convert();
		update(info);
		logger.info("更新联系计划:{}", JacksonUtil.toString(info));
		String callId = contactPlanVo.getCallId();
		if (StrUtil.isNotBlank(callId)) {
			ContactPlanRel contactPlanRel = ContactPlanRel.builder().planId(contactPlanVo.getPlanId()).callId(callId).build();
			if (2 == contactPlanVo.getPlanStatus()) {
				contactPlanRel.setIsHandler(1);
			}
			contactPlanRelMapper.insert(contactPlanRel);
		}
		return contactPlanVo.getPlanId();
	}

	
	public String delContactPlan(String id) {
		ContactPlan info = super.findById(id);
		info.setDataStatus(0);
		updateById(info);
		logger.info("删除联系计划:{}", info);
		return id;
	}

	
	public void exportContactPlan(ContactPlanSearchVo search, HttpServletResponse response) throws IOException {
		List<Object[]> exportList = new ArrayList<>();
		List<ContactPlan> list = contactPlanMapper.selectList(search.lambdaQuery());
		Object[] title = {msg("计划名称"), msg("创建时间"), msg("计划状态")};
		exportList.add(title);
		for (ContactPlan i : list) {
			Object[] row = {i.getPlanName(), i.getCreateTime(), i.getPlanStatus()};
			exportList.add(row);
		}
		csvWriter(exportList, response);
	}

	
	public ContactPlan findById(String id) {
		ContactPlan contactPlan = super.findById(id);
		if (StrUtil.isNotBlank(contactPlan.getVoiceId())) {
			CallVoiceMail callVoiceMail = callVoiceMailMapper.selectByPrimaryKey(contactPlan.getVoiceId());
			if (Objects.nonNull(callVoiceMail) && StrUtil.isNotBlank(callVoiceMail.getFileName())) {
				contactPlan.setFileName(callVoiceMail.getFileName());
			}
		}
		List<ContactPlanRel> contactPlanRels = contactPlanRelMapper.selectList(Wrappers.lambdaQuery(ContactPlanRel.class)
				.eq(ContactPlanRel::getPlanId, contactPlan.getPlanId()));
		if (CollUtil.isNotEmpty(contactPlanRels)) {

			List<CallMakeCall> callMakeCalls = callMakeCallMapper.selectList(Wrappers.lambdaQuery(CallMakeCall.class)
					.in(CallMakeCall::getCallId, contactPlanRels.stream().map(ContactPlanRel::getCallId).collect(Collectors.toList())));
			contactPlan.setCallMakeCalls(callMakeCalls);
		}
		return contactPlan;
	}

	public void invalid(int invalidStep) {

	}
}
